<?php
class ProductDetailAction extends AgriculturalproductsCommonAction {

	//产品明细页面
	public function index() {
		
		$detail_condition['id'] = $_REQUEST ['id'];	
		
		$productVo = $this->CommonSimplequery(0, '', $detail_condition, "product",1,"createdate desc");
		$data['clicktimes'] = $productVo['clicktimes'] + 1 ;
		$Dao = M("product");
		$Dao->where($detail_condition)->save($data);
		
		$this->assign("productVo",
				$this->CommonSimplequery(0, '', $detail_condition, "viewproduct",1,"createdate desc"));
		
		//dump($this->CommonSimplequery(0, '', $detail_condition, "viewproduct",1,"createdate desc"));
				
		//最新上架
		$queryString_lastUpdate = "select * from product where status =1  order by statusdate desc  LIMIT 5";			
		$this->assign("lastUpdateList",$this->CommonSimplequery(1,$queryString_lastUpdate ,'' ,'' ,0,""));
				
		//产品评价
		//$queryString_productComment = " select b.comment_user, b.createdate,b.content from product a ,product_comment b   where a.id = b.product_id and b.comment_user='" .Session::get(C('USER_AUTH_KEY')) ."'" ;			
		$queryString_productComment = " select b.comment_user, b.createdate,b.content from product a ,product_comment b   where a.id = b.product_id and b.product_id = '" .$_REQUEST ['id']. "'" ;
		$this->assign("productCommentList",$this->CommonSimplequery(1,$queryString_productComment ,'' ,'' ,0,""));
		
		//产品评价数量
		$queryString_productCommentCount = " select count(1) as count from product_comment a ,product b where a.product_id = b.id and b.id = '" .$_REQUEST ['id']. "'" ;
		$CountList = $this->CommonSimplequery(1,$queryString_productCommentCount ,'' ,'' ,0,"");		
		$productCommentCount = $CountList['0'];
		$this->assign("productCommentCount",$productCommentCount);
				
		
		
		$this->display();
	}
	
	/**
	 * 
	 * 添加商品至购物车
	 */
	public function addShoppingCart(){
		$Dao = M('shoppingcart');
		$proCondition['id'] = $_REQUEST ['pro_id'] ;
		$productVo = $this->simpleQuery($proCondition, 'product',1);
		
		$data["product_id"] = $_REQUEST ['pro_id'];
		
		
	    $data["user_name"] = Session::get(C('USER_AUTH_KEY')) ;
	    $data["quantity"] = 1 ;	    
	    $data["createdate"] = date('Y-m-d H:i:s',time());
	    $data["lumpsum"] = $productVo['local_price'] ;
	    // 写入数据
	    $Dao->add($data);
		$this->ajaxReturn ( "", "OK！", 1 );
	}
	
	/**
	 * 
	 * 添加评论
	 */
	public function saveProductComment(){
		$id = $_REQUEST ['id'];
		$content = $_REQUEST ['content'];
		$model = M ( "product_comment" );
		$data ["product_id"] = $id;
		$data ["content"] = $content ;
		$data["comment_user"] =  Session::get(C('USER_AUTH_KEY')) ;
		$data ["createdate"] = date('Y-m-d H:i:s',time());		$result = $model->add($data);		 
		$msg = ""  ;
		 
		if ($result==false){
			$msg = "新增评论失败！" . $model->getError();
		}else{
			$msg = "新增评论成功！";
		}
		$this->ajaxReturn ( "111111", $msg, 1 );
	}

}
?>